package org.example.service;

import com.google.protobuf.ByteString;
import com.google.protobuf.ProtocolStringList;
import io.grpc.stub.StreamObserver;
import org.example.HelloServiceGrpc;
import org.example.helloProto;

/**
 * @Author : 叶荻风
 * @create : 2024/6/20 11:06
 */
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase{
    @Override
    public void hello(helloProto.HelloRequest request, StreamObserver<helloProto.HelloResponse> responseObserver) {
        //1.接受client的请求参数
        String name = request.getUsername();
        //2.业务处理
        System.out.println("业务处理，打印入参: "+name);
        //3.封装响应
        helloProto.HelloResponse helloResponse =
                helloProto.HelloResponse.newBuilder().setUsername("业务处理完毕ack "+name).build();
        responseObserver.onNext(helloResponse); //处理后的消息，通过网络，回传client
        responseObserver.onCompleted(); //所有消息回传完毕
    }

    @Override
    public StreamObserver<helloProto.HelloRequest> s2cs(StreamObserver<helloProto.HelloResponse> responseObserver) {
        return new StreamObserver<helloProto.HelloRequest>() {
            @Override
            public void onNext(helloProto.HelloRequest helloRequest) {
                System.out.println("接收到了client发来的消息"+helloRequest.getUsername());
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                System.out.println("client所有消息都发送到了客户端 ....");
                helloProto.HelloResponse response = helloProto.HelloResponse.newBuilder().setUsername("处理完成").build();
                responseObserver.onNext(response);
                responseObserver.onCompleted();
            }
        };

    }

    @Override
    public StreamObserver<helloProto.HelloRequest> cs2ss(StreamObserver<helloProto.HelloResponse> responseObserver) {
        return new StreamObserver<helloProto.HelloRequest>() {
            @Override
            public void onNext(helloProto.HelloRequest value) {
                System.out.println("从client上接受到数据"+value.getUsername());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                responseObserver.onNext(helloProto.HelloResponse.newBuilder().setUsername("response "+value.getUsername()+" result").build());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {
                System.out.println("接受到了所有的请求消息....");
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public void c2ss(helloProto.HelloRequest request, StreamObserver<helloProto.HelloResponse> responseObserver) {
        //1.接受请求数据
        String name  = request.getUsername();
        //2.业务处理
        System.out.println("name = "+name);
        //3.更具处理结果，响应
        for (int i=0;i<10;i++){
            helloProto.HelloResponse.Builder builder = helloProto.HelloResponse.newBuilder();
            builder.setUsername("处理结果"+i);
            helloProto.HelloResponse response = builder.build();

            responseObserver.onNext(response);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        responseObserver.onCompleted();

    }

    @Override
    public void hello1(helloProto.HelloRequest1 request, StreamObserver<helloProto.HelloResponse1> responseObserver) {
        //1.接受client的请求参数
        ProtocolStringList nameList = request.getNameList();
        //2.业务处理
        System.out.println("业务处理，打印入参: "+nameList.asByteStringList());
        //3.封装响应
        helloProto.HelloResponse1.Builder builder = helloProto.HelloResponse1.newBuilder();
        for(ByteString name:nameList.asByteStringList()){
            builder.addResult( String.valueOf(name));
        }
        helloProto.HelloResponse1 response1 = builder.build();
        responseObserver.onNext(response1);
        responseObserver.onCompleted();
    }
}
